<?php

    // This is PHP5 Code, by the way.
   // $dir=getcwd();
    //echo $dir;
    require_once("Logger/logs.php");
    require_once("defines.php");
    //require_once("Functions/Functions.php");
	require_once("SocketServer/SocketServer.php"); // Include the Class File
	
    //error_reporting(E_ALL ^ E_NOTICE);
  // error_reporting(E_ALL ^ E_DEPRECATED);
  //  error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);    
   error_reporting(E_ALL &~ E_NOTICE &~ E_DEPRECATED);
   // error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);  //Todos los errores
    
    ini_set('error_reporting', E_ALL);

    $objLogger = new Logger();
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    //echo "IPSOCK: ".IPSOCKET."<br>";  //FD debug
  

 ?>

<table>
    <tr>
        <td>
            <form id="formServBt" name="nameFormServBt" method="POST" >
                <button name="btStartServ" id="idStartBt">START</button>
                <button name="btStopServ" id="idStopBt">STOP</button>
            </form>
        </td>
    </tr>
</table>


 
 <?

    $objLogger->write_log("Iniciando servicio...");
    
    $server = new SocketServer(IPSOCKET,9999); // Create a Server binding to the default IP address (null) and listen to port 31337 for connections
    //$server = new SocketServer(null,31337); // Create a Server binding to the default IP address (null) and listen to port 31337 for connections
	$server->max_clients = 10; // Allow no more than 10 people to connect at a time
	$server->hook("CONNECT","handle_connect"); // Run handle_connect everytime someone connects
	$server->hook("INPUT","handle_input"); // Run handle_input whenever text is sent to the server
    
/////////////////////////////// BOTON ACTION //////////////////////////////////
    if(isset($_POST['btStartServ']))
    {    
        try
        {
           if(isset($_SESSION['SocketThread'])==true)
           {
                echo '<script>alert("Socket thread ya iniciado");</script>';
           }else
           {
            
                $_SESSION['SocketThread']=true;
                echo "<br>Conectando Server...";
                echo '<script>alert("Conectando Socket thread server");</script>';
                //sleep(1);
                //SocketServer::disconnect($client->server_clients_index,"GoodBye");
                $server->infinite_loop(); // Run Server Code Until Process is terminated. //FD 
                     
            }    
        }catch(Exception $ex)
        {
            Logger::write_log("Exception Server: ".$ex);
            
        }
    }
    
    if(isset($_POST['btStopServ']))
    {    
        try
        {
            if(isset($_SESSION['SocketThread'])==true)
            {
                echo "Desconectando Server";
                $_SESSION['SocketThread']=false;
                
                
                $meta = stream_get_meta_data($fp);
               
               // print_r($meta);
                //header("Content-Length: 0");
                //header("Connection: close");
                //flush();
                socket_Shutdown($client->server_clients_index,2);
                
                SockClose($client->server_clients_index,"Socket Close");    //FD
                
                
                SocketServer::disconnect($client->server_clients_index,"GoodBye");
                
            }else
            {
                echo '<script>alert("Socket Desconectado");</script>';
            }
    
        }catch(Exception $ex)
        {
            Logger::write_log("Exception Disconnect Server: ".$ex);
            
        }
    }
    
?>